<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
	<meta charset="utf-8">
	<title>购物车</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
	<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">
	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" href="${ctxStatic}/shop/css/easyLife/font-awesome.min.css">
	<link rel="stylesheet" href="${ctxStatic}/shop/css/easyLife/aui.css">
	<link rel="stylesheet" href="${ctxStatic}/shop/css/easyLife/main.css">
	<link rel="stylesheet" href="${ctxStatic}/shop/css/easyLife/cart.css">
	<link rel="stylesheet" href="${ctxStatic}/shop/css/easyLife/theme-color.css">
	<!--[if lt IE 9]>
	<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
	<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
	<![endif]-->
	<style>

	</style>
</head>
<body>
<form method="post" name="cart_form" target="_self" id="cart_form" action="">
	<header class="zyw-header">
		<div class="zyw-container white-color">
			<div class="head-l"><a href="javascript:window.history.back(-1)" target="_self"><img src="<%=basePath%>/static/shop/images/head-return.svg" alt=""></a></div>
			<h1>购物车</h1>
			<div class="head-r"><a href="">编辑</a></div>
		</div>
	</header>
	<footer class="zyw-footer">
		<div class="zyw-container white-bgcolor">
			<div class="settle_box">
				<dl class="all_check select">
					<dt>
						<span id="all_pitch_on"></span>
						<em>全选</em>
					</dt>
				</dl>
				<dl class="total_amount">
					<dt>
						合计：
						<p id="total_price">
							¥<b>0</b>
						</p>
					</dt>
					<dd>不含运费</dd>
				</dl>
				<input type="hidden" name="gcs" id="gcs"/>
				<a class="settle_btn" href="javascript:void(0);" id="confirm_cart" onclick="account_cart()">去结算</a>
				<a class="settle_btn" href="javascript:void(0);" id="confirm_cart1" onclick="big_cart_remove()">删除</a>
			</div>
		</div>
	</footer>
	<section class="zyw-container">
		<!--list-->
		<div class="commodity_list_box">
			<div class="cart_top">
				<span>商品清单</span>
				<p id="weights"></p>
				<div class="clear"></div>
			</div>
			<!--商品列表-->
			<c:forEach items="${list}" var="shopcar">

				<div class="commodity_box" ontouchstart="gtouchstart()" ontouchmove="gtouchmove()" ontouchend="gtouchend()">
					<div class="commodity_list">
						<!--店名信息-->
						<div class="tite_tim select">
							<em aem="1" cart_id="${shopcar.goods.id}" id="aaaaa"></em>
							<span>${shopcar.goods.store}</span>
							<div class="clear"></div>
						</div>
						<!--商品-->
						<ul class="commodity_list_term">
							<li class="select">
								<em aem="0" cart_id="${shopcar.goods.id}" class="goodsDetail"></em>
								<img src="${shopcar.goods.img}"/>
								<div class="div_center">
									<h4>${shopcar.goods.name}</h4>
									<span>${shopcar.goods.content}</span>
									<p class="now_value">
										<i>￥</i>
										<b class="qu_su">${shopcar.goods.price}</b>
									</p>
								</div>
								<input type="hidden" value="${shopcar.id}" id="cart_id">
								<div class="div_right">
									<i onclick="reducew(this)">-</i>
									<span class="zi">1</span>
									<input type="hidden" value="84">
									<i onclick="plusw(this)">+</i>
								</div>
							</li>

						</ul>
						<!--优惠信息-->
						<div class="shop_ul_bottom account_info_box">
							<ul class="account_info">
								<li class="i_text">
									<span class="info_name xi_cu">包邮</span>
									<span class="info_name">商家包邮</span>
								</li>
							</ul>
						</div>
					</div>
				</div>
			</c:forEach>
			<!-- 商品列表 end -->
		</div>
		<!-- end -->
	</section>
</form>
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/shop/js/easyLife/ionic.bundle.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/shop/js/easyLife/runend.js"></script>
<script src="${ctxStatic}/shop/js/easyLife/bootstrap.min.js"></script>

<script type="text/javascript">
    var timeOutEvent=0;//定时器
    //开始按
    function gtouchstart(){
        timeOutEvent = setTimeout("longPress()",500);//这里设置定时器，定义长按500毫秒触发长按事件，时间可以自己改，个人感觉500毫秒非常合适
        return false;
    };
    //手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
    function gtouchend(){
        clearTimeout(timeOutEvent);//清除定时器
        if(timeOutEvent!=0){
            //这里写要执行的内容（尤如onclick事件）

        }
        return false;
    };
    //如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
    function gtouchmove(){
        clearTimeout(timeOutEvent);//清除定时器
        timeOutEvent = 0;

    };

    //真正长按后应该执行的内容
    function longPress() {
        timeOutEvent = 0;
        //执行长按要执行的内容，如弹出菜单
        alert("长按事件触发发");
        var id=document.getElementById("cart_id").value;
        $.ajax({

				type:"post",
				url:"cart",
				async:false,
				data:{"id":id},
				dataType:"text",
				success:function(data){
				alert("删除成功");

		}
		});
    }
</script>
<script type="text/javascript">

	//定义全局变量
	var i=0;

	//金额总和
	var money=0;

	//计算合计价格
	var cart_money=new Object();

	//全部商品ID
	var cart_id=new Object();
	//备份商品ID，用于全选后去掉全选又再次全选
	var cart_id_copy=new Object();

</script>
<script>
	var noX = 0;
	/* 没选中时点击加减计算数量  */
	var allThis = $(".commodity_box .select em");
	/*底部全选*/
	var totalH;
	/* 减  */
	function reduceMod(e, totalH, mod, noX) {
		var tn = e.siblings().find(".qu_su").text();
		/* 当前选中商品  */
		var tn1 = e.siblings().find(".zi").text();
		/* 商品数量  */
		if (mod != 2) {
			var Total = parseFloat(totalH) - (tn * tn1);
			/* 总价格减该商品总数价格  */
			$("#total_price b").text(Total.toFixed(2));
		} else {
			/* 合计加单价-1 */
			var Total = parseFloat(totalH) - parseFloat(tn);
			/* 总价格减该商品总数价格  */
			$("#total_price b").text(Total.toFixed(2));
		}

	}
	;/* 加  */
	function plusMod(e, totalH, mod) {
		var tn = e.siblings().find(".qu_su").text();
		/* 当前选中商品  */
		var tn1 = e.siblings().find(".zi").text();
		/* 商品数量  */
		if (mod != 2) {
			var Total = parseFloat(totalH) + (tn * tn1);
			/* 总价格加上该商品总数价格  */
			$("#total_price b").text(Total.toFixed(2));
		} else {
			/* 合计加单价+1 */
			var Total = parseFloat(totalH) + (parseFloat(tn) + (noX - 1));
			/* 总价格加上该商品总数价格  */
			$("#total_price b").text(Total.toFixed(2));
		}

	}
	;/*全选该店商品价格 加*/
	function commodityPlusMod(e, totalH) {
		var qu = e.parents(".commodity_list").find(".pitch_on").parent().find(".qu_su");
		var quj = e.parents(".commodity_list").find(".pitch_on").parent().find(".zi");
		var Total = 0;
		var erTotal = true;
		/* 该商品全部金额  */
		for (var i = 0; i < qu.length; i++) {
			var n = qu.eq(i).text();
			var n1 = quj.eq(i).text();
			/*合计价格*/
			if (erTotal) {
				Total = parseFloat(totalH) + (parseFloat(n) * parseFloat(n1));
				if (Total < 0)
					Total = 0;
				erTotal = false;
			} else {
				Total = parseFloat(Total) + (parseFloat(n) * parseFloat(n1));

			}
		}
		$("#total_price b").text(Total.toFixed(2));
		/* 合计金额  */
	}
	;var plus;
	/*全选该店商品价格 减*/
	function commodityReduceMod(e, totalH) {
		var qu = e.parents(".commodity_list").find(".pitch_on").parent().find(".qu_su");
		var quj = e.parents(".commodity_list").find(".pitch_on").parent().find(".zi");
		var Total = 0;
		plus = totalH;

		var erTotal = true;
		/* 该商品全部金额  */
		for (var i = 0; i < qu.length; i++) {
			var n = qu.eq(i).text();
			var n1 = quj.eq(i).text();
			/*合计价格*/
			if (erTotal) {
				Total = parseFloat(totalH) - (parseFloat(n) * parseFloat(n1));
				plus = Total;
				if (Total < 0)
					Total = 0;
				erTotal = false;
			} else {
				Total = parseFloat(Total) - (parseFloat(n) * parseFloat(n1));
				plus = Total;

			}

			$("#total_price b").text(Total.toFixed(2));
			/* 合计金额  */
			plus;
		}
	}
	;/*全部商品价格*/
	function commodityWhole() {
		/* 合计金额  */
		var je = $(".commodity_box .select .qu_su");
		/* 全部商品单价  */
		var je1 = $(".commodity_box .select .zi");
		/* 全部商品数量  */
		var TotalJe = 0;
		for (var i = 0; i < je.length; i++) {
			var n = je.eq(i).text();
			var n1 = je1.eq(i).text();
			TotalJe = TotalJe + (parseFloat(n) * parseFloat(n1));

		}
		$("#total_price b").text(TotalJe.toFixed(2));
		/* 合计金额  */
	}
	;
	//选择结算商品

	$(".select em").click(function() {
		var su = $(this).attr("aem");
		var carts_id = $(this).attr("cart_id");
		totalH = $("#total_price b").text();
		/* 合计金额  */
		if (su == 0) {
			/* 单选商品  */
			if ($(this).hasClass("pitch_on")) {
				/*去该店全选*/
				$(this).parents("ul").siblings(".select").find("em").removeClass("pitch_on");
				/*去底部全选*/
				$("#all_pitch_on").removeClass("pitch_on");
				$(this).removeClass("pitch_on");
				reduceMod($(this), totalH);
				cart_id[carts_id] = "";
				delete cart_id[carts_id];
			} else {
				$(this).addClass("pitch_on");
				var n = $(this).parents("ul").children().find(".pitch_on");
				var n1 = $(this).parents("ul").children();
				plusMod($(this), totalH, 0, noX);
				cart_id[carts_id] = "";
				/*该店商品全选中时*/
				if (n.length == n1.length) {
					$(this).parents("ul").siblings(".select").find("em").addClass("pitch_on");
				}
				/*商品全部选中时*/
				var fot = $(".commodity_list_box .tite_tim .pitch_on");
				var fot1 = $(".commodity_list_box .tite_tim em");
				if (fot.length == fot1.length)
					$("#all_pitch_on").addClass("pitch_on");
			}
		} else {
			/* 全选该店铺  */
			if ($(this).hasClass("pitch_on")) {
				/*去底部全选*/
				$("#all_pitch_on").removeClass("pitch_on");
				$(this).removeClass("pitch_on");

				commodityReduceMod($(this), totalH);
				$(this).parent().siblings("ul").find("em").removeClass("pitch_on");
				delete cart_id[carts_id];
			} else {
				commodityReduceMod($(this), totalH);

				$(this).addClass("pitch_on");

				$(this).parent().siblings("ul").find("em").addClass("pitch_on");

				/*if(plus != NaN && plus != undefined && plus > 0){
                 totalH = parseFloat(totalH)-parseFloat(plus);
                 if(totalH < 0)
                 totalH = 0;
                 }*/
				if (plus == undefined || plus == NaN) {
					plus = 0
				}


				commodityPlusMod($(this), plus);
				cart_id[carts_id] = "";
				/*商品全部选中时*/
				var fot = $(".commodity_list_box .tite_tim .pitch_on");
				var fot1 = $(".commodity_list_box .tite_tim em");
				if (fot.length == fot1.length) {
					$("#all_pitch_on").addClass("pitch_on");
				}

			}
		}

		//计算选择数值
		//number();

	});
	/* 底部全选  */

	var bot = 0;
	$("#all_pitch_on").click(function() {
		if (bot == 0) {
			$(this).addClass("pitch_on");
			allThis.removeClass("pitch_on");
			allThis.addClass("pitch_on");
			/*总价格*/
			commodityWhole();
			bot = 1;
			//重新加入属性对象
			for (var key in cart_id_copy) {
				cart_id[key] = "";
			}
		} else {
			$(this).removeClass("pitch_on");
			allThis.removeClass("pitch_on");
			$("#total_price b").text("0");
			bot = 0;
			//移除全部对象
			for (var key in cart_id) {
				delete cart_id[key];
			}
		}
		//计算选择数值
		//number();
	});

	function number() {
		var num = 0;
		for (var key in cart_id) {
			num++;
		}
		//将选择的放入到计算里面
		//$("#confirm_cart").html("结算("+num+")");
	}

	/* 编辑商品  */
	var topb = 0;
	$("#rem_s").click(function() {
		if (topb == 0) {
			$(this).text("完成");
			$(".total_amount").hide();
			/* 合计  */
			$("#confirm_cart").hide();
			/* 结算  */
			$("#confirm_cart1").show();
			/* 删除 */
			topb = 1;
		} else {
			topb = 0;
			$(this).text("编辑");
			$(".total_amount").show();
			/* 合计  */
			$("#confirm_cart").show();
			/* 结算  */
			$("#confirm_cart1").hide();
			/* 删除 */
			allThis.removeClass("pitch_on");
			/* 取消所有选择  */
			$("#all_pitch_on").removeClass("pitch_on");
			/* 取消所有选择  */
			$("#total_price b").text("0");
			/*合计价格清零*/

		}

	});
	/* 加减  */

	function reducew(obj) {
		//减
		var $this = $(obj);
		var totalH = $("#total_price b").text();
		/* 合计金额  */
		var ise = $this.siblings("span").text();
		var gc_id = $this.siblings("input").val();
		if (noX <= 0) {
			noX = 0;
		} else {
			noX--;
		}
		;
		if (parseInt(ise) <= 1) {
			$this.siblings("span").text("1");
		} else {
			var n = parseInt(ise) - 1;
			$this.siblings("span").text(n);
			if ($this.parent().parent().children("em").hasClass("pitch_on")) {
				var mo = $this.parent().parent().children("em");
				reduceMod(mo, totalH, 2, noX);
				noX = 0;
			}

		}

		//goods_count_adjust(gc_id,n,null);
	}
	;
	function plusw(obj) {
		//加
		var $this = $(obj);
		var totalH = $("#total_price b").text();
		/* 合计金额  */
		var ise = $this.siblings("span").text();
		var gc_id = $this.siblings("input").val();
		var n = parseInt(ise) + 1;
		noX++;

		$this.siblings("span").text(n);
		if ($this.parent().parent().children("em").hasClass("pitch_on")) {
			var mo = $this.parent().parent().children("em");
			plusMod(mo, totalH, 2, noX);
			noX = 0;
		}

		//goods_count_adjust(gc_id,n,null);
	}

	//删除
	function big_cart_remove() {
		$(".commodity_list_term .pitch_on").parent().remove();
		$(".commodity_list .tite_tim > em.pitch_on").parents(".commodity_box").remove();
	}
</script>
<script>
function account_cart() {
	//获取所有商品对象
	var list=$(".goodsDetail.pitch_on");
	var jsonMap="";
	for (let i=0;i<list.length;i++) {
		var goodsId=$(list.get(i)).attr("cart_id");
		console.log(i+"---"+goodsId);
		var goodsNum=$(list.get(i).parentElement).find(".zi").html();//获取到该商品的数量
		console.log(i+"---"+goodsNum);
		var shopcarId=$(list.get(i).parentElement).find("#cart_id").value;
		//拼接map
		var map="{goodsId:"+goodsId+",goodsNum:"+goodsNum+",shopcarId:"+shopcarId+"}";
		console.log(map);
		jsonMap+=map+"";
	}
	jsonMap="["+jsonMap+"]";
	console.log(jsonMap);
	$.ajax({
		url:"jiesuan",
		data:{"mapList":jsonMap},
		success:function(){
			console.log("------");
		}
	});

}

</script>
</body>
</html>